<template>
  <div>
    <tableComponents :pagination="{ currentPage, pageSize, total: list.all_num }" @handleCurrentChange="handleCurrentChange"
        @handleSizeChange="handleSizeChange" @sendUserSearch="getSearch" :searchData="userSearchData" :isHaveExport="false" @toAdd="editMovies">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list.data" stripe v-loading="loading" header-row-class-name="head_color" tooltip-effect="dark"
            style="width: 99.8%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" class="tc" width="35"></el-table-column>

          <el-table-column prop="sort" label="排序" min-width="50">
            <template slot-scope="scope">
              <el-input @change="sortNew(scope.row)" v-model.number="scope.row.sort" autocomplete="off" size="small"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="封面图" min-width="100" align="center">
            <template slot-scope="scope">
              <el-popover placement="right" title trigger="hover">
                <img :src="scope.row.piclink" style="width:300px;" />
                <img slot="reference" :src="scope.row.piclink" :alt="scope.row.piclink" style="width:100px" />
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="影视信息" min-width="200">
            <template slot-scope="scope">
              <div class="user_table">
                <span>影视标题：</span>
                <small>{{ scope.row.title || "----" }}</small>
                <br />
                <span>演员：</span>
                <small>{{ scope.row.performer || "----" }}</small>
                <br />
                <span>导演：</span>
                <small>{{ scope.row.director || "----" }}</small>
                <br />
                <span>类型：</span>
                <small>{{ scope.row.type_cn || "----" }}</small>
                <br />
                <span>地区：</span>
                <small>{{ scope.row.region_cn || "----" }}</small>
                <br />
                <span>年份：</span>
                <small>{{ scope.row.year_cn || "----" }}</small>
                <br />
              </div>
            </template>
          </el-table-column>

          <el-table-column label="其他信息" min-width="120">
            <template slot-scope="scope">
              <div class="user_table">
                <span>评分：</span>
                <small>{{ scope.row.score }}</small>
                <br />
                <span>点赞数：</span>
                <small>{{ scope.row.likes }}</small>
                <br />
                <span>转发数：</span>
                <small>{{ scope.row.share }}</small>
                <br />
                <span>观看数：</span>
                <small>{{ scope.row.watch }}</small>
                <br />
                <span>观看权限：</span>
                <small>{{ scope.row.watch_flag || "----" }}</small>
                <br />
              </div>
            </template>
          </el-table-column>

          <el-table-column label="是否推荐" min-width="80" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.index" active-color="#1aad19" inactive-color="#ffffff" active-text="推荐"
                  inactive-text="不推荐" active-value="1" @change="getIndex($event, scope.row)" inactive-value="0"></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="是否显示" min-width="80" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-switch class="zdySwith" :width="82" v-model="scope.row.show" active-color="#1aad19" inactive-color="#ffffff" active-text="显示"
                  inactive-text="不显示" active-value="1" @change="getShow($event, scope.row)" inactive-value="0"></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button :loading="$store.state.isLoading" size="mini" @click="seeIntro(scope.row)" type="primary">简介</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" @click="editMovies(scope.row)" type="primary">编辑</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="deleteNew(scope.row.id)">删除</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <el-button :loading="$store.state.isLoading" type="primary" size="mini" @click="deleteAll">删除</el-button>
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" :title="Movies.id ? '编辑' : '添加'" :modal-append-to-body="false" :visible.sync="showMovies"
        class="tl size_13" width="800px">
      <el-form :model="Movies" class="mar_top" label-width="100px">
        <el-form-item label="分类" required>
          <el-col :span="10">
            <el-select v-model="Movies.cate_id" @change="get_cate_son">
              <el-option v-for="item in Movies_cate" :key="item.id" :label="item.title" :value="item.id"></el-option>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="标题">
          <el-col :span="10">
            <el-input v-model="Movies.title" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="导演">
          <el-col :span="10">
            <el-input v-model="Movies.director" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="演员">
          <el-col :span="10">
            <el-input type="textarea" v-model="Movies.performer" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="类型">
          <el-col :span="10">
            <el-select v-model="Movies.type">
              <el-option v-for="item in Movies_type" :key="item.id" :label="item.title" :value="item.id"></el-option>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="地区">
          <el-col :span="10">
            <el-select v-model="Movies.region">
              <el-option v-for="item in Movies_region" :key="item.id" :label="item.title" :value="item.id"></el-option>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="年份">
          <el-col :span="10">
            <el-select v-model="Movies.year">
              <el-option v-for="item in Movies_year" :key="item.id" :label="item.title" :value="item.id"></el-option>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="点赞数">
          <el-col :span="10">
            <el-input v-model="Movies.likes" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="转发数">
          <el-col :span="10">
            <el-input v-model="Movies.share" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="观看数">
          <el-col :span="10">
            <el-input v-model="Movies.watch" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="观看权限">
          <el-col :span="10">
            <el-input v-model="Movies.watch_flag" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="播放地址">
          <el-col :span="20">
            <el-input v-model="Movies.url" clearable></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="简介">
          <el-col :span="16">
            <el-input type="textarea" :rows="5" v-model="Movies.introduction" clearable></el-input>
          </el-col>
        </el-form-item>

        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button :loading="$store.state.isLoading" size="medium" type="primary" @click.prevent="subMovies">确定</el-button>
        </div>
      </el-form>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" title="简介" :modal-append-to-body="false" :visible.sync="showIntro" class="tl size_13" width="800px">
      <el-form :model="introduction" class="mar_top" label-width="100px">
        <el-form-item>
          <el-col :span="16">{{ introduction.introduction }}</el-col>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { moviesListSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data () {
    return {
      userSearchData: moviesListSearchData,
      loading: false,
      list: [],
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      Movies: {},
      multipleSelection: [],
      showMovies: false,
      showIntro: false,
      introduction: [],
      Movies_cate: [],
      Movies_type: [],
      Movies_region: [],
      Movies_year: []
    };
  },
  components: {},
  methods: {
    getShow (val, row) {
      var params = {};
      params = row;
      params.show = val;
      this.moviesEdit(params);
    },
    getIndex (val, row) {
      var params = {};
      params = row;
      params.index = val;
      this.moviesEdit(params);
    },
    sortNew (row) {
      this.moviesEdit(row);
    },
    seeIntro (item) {
      this.showIntro = true;
      this.introduction = item;
    },
    editMovies (item) {
      if (item) {
        this.Movies = item;
        if (item.cate_id) {
          this.get_cate_son(item.cate_id);
        }
      } else {
        this.Movies = {};
      }
      this.showMovies = true;
    },
    subMovies () {
      if (this.Movies.id) {
        this.moviesEdit(this.Movies);
      } else {
        this.$api.getMovies.movies_saveadd(this.Movies).then(res => {
          if (res.data.code == 200) {
            this.$fnc.alertSuccess("操作成功");
            this.showMovies = false;
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      }
    },
    moviesEdit (data) {
      var params = {};
      params = data || {};
      this.$api.getMovies.movies_saveedit(params).then(res => {
        if (res.data.code == 200) {
          this.showMovies = false;
          this.getNewsAll(this.searchAll);
        } else {
          this.$fnc.alertError(res.data.result);
        }
      });
    },
    getSearch (obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll (data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;

      this.$api.getMovies.movies_lists(params).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    handleSizeChange: function (size) {
      var params = this.searchAll;
      params.page = 1;
      params.page_size = size;
      this.pageSize = size;
      this.currentPage = 1;
      this.getNewsAll(params);
    },
    handleCurrentChange: function (currentPage) {
      var params = this.searchAll;
      params.page = currentPage;
      params.page_size = this.pageSize;
      this.currentPage = parseInt(currentPage);
      this.getNewsAll(params);
    },
    deleteAll () {
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            var data = { id_str: str };
            this.$api.getMovies.movies_del(data).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这些信息。", "success");
                this.getNewsAll(this.searchAll);
              } else {
                this.$swal("删除失败！", res.data.result, "error");
              }
            });
          }
        });
      }
    },
    deleteNew (id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id_str: id };
          this.$api.getMovies.movies_del(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    },
    get_cate_list () {
      this.$api.getMovies.movies_cate_lists_tree({ parent_id: 0 }).then(res => {
        if (res.data.code == 200) {
          this.Movies_cate = res.data.result;
          this.$store.commit("setMoviesCate", res.data.result);
        }
      });
    },
    get_cate_son (val) {
      this.Movies_type = [];
      this.Movies_region = [];
      this.Movies_year = [];
      this.$api.getMovies
        .movies_cate_lists_tree({ parent_id: val })
        .then(res => {
          if (res.data.code == 200) {
            this.Movies_type =
              res.data.result && res.data.result[0] ? res.data.result[0].z : [];
            this.Movies_region =
              res.data.result && res.data.result[1] ? res.data.result[1].z : [];
            this.Movies_year =
              res.data.result && res.data.result[2] ? res.data.result[2].z : [];
          }
        });
    }
  },
  created () {
    this.getNewsAll();
    this.get_cate_list();
  }
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
.user_table span {
  font-size: 13px;
}
</style>
